


package com.everyline.commonlibrary.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.widget.EditText;
import android.widget.ImageView;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.LogUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.FutureTarget;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;

import java.io.File;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;


/**
 * 文件描述：
 * 作者：G
 * 创建时间：2019/8/6
 * * 参考设置：http://www.tuicool.com/articles/3Af6Zby
 * *DiskCacheStrategy.NONE:什么都不缓存
 * *DiskCacheStrategy.SOURCE:仅缓存原图(全分辨率的图片)
 * *DiskCacheStrategy.RESULT:仅缓存最终的图片,即修改了尺寸或者转换后的图片
 * *DiskCacheStrategy.ALL:缓存所有版本的图片,默认模式
 * *
 * * url =    url.replaceAll("http:/", "http://");
 */
public class ImageLoaderHelper {
//    public static int IMG_LOADING = R.drawable.ic_image_loading;
//    public static int IMG_ERROR = R.drawable.ic_empty_picture;

    private static volatile ImageLoaderHelper instance;

    private ImageLoaderHelper() {

    }

    public static ImageLoaderHelper getInstance() {

        if (instance == null) {
            synchronized (ImageLoaderHelper.class) {
                if (instance == null) {
                    instance = new ImageLoaderHelper();
                }
            }
        }

        return instance;
    }

//    private int optionsInit(int flag) {
//        int sourceId = 0;
//        if (flag == 0){
//            sourceId = R.drawable.ic_image_loading ;
//        }
//        return sourceId ;
//    }

    public void load(Context context, String url, ImageView iv) {

        if (iv != null && context != null && url != null) {
            RequestOptions options = new RequestOptions()
//                    .placeholder(optionsInit(0))  //加载中显示的图片
                    .centerCrop()       //图像则位于视图的中央
                    // .override(1090, 1090*3/4)
//                    .error(IMG_ERROR) //加载失败时显示的图片centerCrop().
                    .diskCacheStrategy(DiskCacheStrategy.ALL);  //图片缓存
            Glide.with(context).load(url).apply(options).into(iv);
        }

    }

    public void loadGif(Context context, String url, ImageView iv) {

        if (iv != null && context != null && url != null) {
            RequestOptions options = new RequestOptions()
//                    .placeholder(optionsInit(0))  //加载中显示的图片
                    .centerCrop()       //图像则位于视图的中央
                    // .override(1090, 1090*3/4)
//                    .error(IMG_ERROR) //加载失败时显示的图片centerCrop().
                    .diskCacheStrategy(DiskCacheStrategy.ALL);  //图片缓存
            Glide.with(context).asGif().load(url).apply(options).into(iv);
        }

    }


    public void loadCircleCrop(Context context, String url, ImageView iv) {

        if (iv != null && context != null && url != null) {
            RequestOptions options = new RequestOptions()
//                    .placeholder(optionsInit(0))  //加载中显示的图片
                    .centerCrop()       //图像则位于视图的中央
                    .bitmapTransform(new CircleCrop())
                    // .override(1090, 1090*3/4)
//                    .error(IMG_ERROR) //加载失败时显示的图片centerCrop().
                    .diskCacheStrategy(DiskCacheStrategy.ALL);  //图片缓存
            Glide.with(context).load(url).apply(options).into(iv);
        }

    }

    public void loadRadius(Context context, String url, ImageView iv, int radius) {


        if (iv != null && context != null && url != null) {
            RequestOptions options = new RequestOptions()
//                    .placeholder(IMG_LOADING)
                    .diskCacheStrategy(DiskCacheStrategy.ALL);
//            Glide.with(context).load(url).apply(options).into(iv);
            Glide.with(context).load(url)
                    .apply(RequestOptions.bitmapTransform(new RoundedCorners(radius)))
                    .into(iv);
        }

    }

    public void load(Context context, Uri url, ImageView iv) {


        if (iv != null) {
            RequestOptions options = new RequestOptions()
//                    .placeholder(IMG_LOADING)
                    .centerCrop()
                    .diskCacheStrategy(DiskCacheStrategy.ALL);
            Glide.with(context).load(url).apply(options).into(iv);
        }

    }


    public void load(Context context, File fileName, ImageView iv) {

        if (iv != null) {
            RequestOptions options = new RequestOptions()
//                    .placeholder(IMG_LOADING)
                    .centerCrop()//centerCrop是利用图片图填充ImageView设置的大小
                    .diskCacheStrategy(DiskCacheStrategy.ALL);
            Glide.with(context).load(fileName).apply(options).into(iv);
        }

    }

    /**
     * 加载资源图片
     *
     * @param context
     * @param resourceId
     * @param iv
     */
    public void load(Context context, Integer resourceId, ImageView iv) {

        if (iv != null) {
            RequestOptions options = new RequestOptions()
//                    .placeholder(IMG_LOADING)
                    .diskCacheStrategy(DiskCacheStrategy.ALL);
            Glide.with(context).load(resourceId).apply(options).into(iv);
        }

    }

    /**
     * 用rxjava 实现 网络图片下载到本地
     *
     * @param context
     * @param imgPath 图片网络地址
     */
    private void saveImageToGallery(Context context, String imgPath) {

        Observable.create(new ObservableOnSubscribe<Boolean>() {

            @Override
            public void subscribe(ObservableEmitter<Boolean> emitter) throws Exception {

                FutureTarget<File> futureTarget = Glide.with(context).load(imgPath).downloadOnly(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL);
                try {

                    File fileImg = futureTarget.get();
                    File file = context.getExternalCacheDir();
                    String fileName = "image";
                    File appDir = new File(file, fileName);
                    if (!appDir.exists()) {
                        appDir.mkdir();
                    }
                    String imgName = System.currentTimeMillis() + ".jpg";
                    File destFile = new File(appDir, imgName);
                    LogUtils.d("destFile===", destFile.getAbsolutePath());
                    boolean result = FileUtils.copy(fileImg, destFile);
                    emitter.onNext(result);
                } catch (Exception e) {
                    emitter.onNext(false);
                }

            }
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer<Boolean>() {
            @Override
            public void accept(Boolean aBoolean) throws Exception {
                if (aBoolean) {
                    LogUtils.d("destFile===", "图片保存成功");
                } else {
                    LogUtils.d("destFile===", "图片保存失败");
                }
            }
        });

    }


}


